package cn.itcast.mq.listen;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SpringAMQListener {
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg){
//        log.info("Spring消费者接收到的消息" + msg);
//    }

    private static long work1Num = 0;
    private static long work2Num = 0;

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueueMessage1(String msg) throws InterruptedException {
        work1Num++;
        log.info("Spring消费者1接收到的消息 -> {} ,数量为:{}" + msg,work1Num);
        TimeUnit.MILLISECONDS.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueueMessage2(String msg) throws InterruptedException {
        work2Num++;
        log.warn("Spring消费者2接收到的消息 -> {} , 数量为:{}" + msg,work2Num);
        TimeUnit.MILLISECONDS.sleep(200);
    }


    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue1(Map msg) throws InterruptedException {
        log.warn("消费者1接收到的消息 -> 【{}】 " + msg);
    }


    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException {
        log.warn("消费者1接收到的消息 -> 【{}】 " + msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) throws InterruptedException {
        log.warn("消费者2接收到的消息 -> 【{}】 " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"blue","red"}
    ))
    public void listenDirectQueue1(String msg) throws InterruptedException {
        log.warn("消费者1接收到的消息 -> 【{}】 " + msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct2.queue"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"yellow","red"}
    ))
    public void listenDirectQueue2(String msg) throws InterruptedException {
        log.warn("消费者2接收到的消息 -> 【{}】 " + msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息：【" + msg + "】");
    }






}
